const app = getApp();
Page({
  data:{
    companyName :'',
    logoPath :'',
    phone:'',
    loading:false,
  },
  onShow:function(){
    const _this = this;
    // 最近登录
    wx.getStorage({
      key: 'loginPhone',
      success: function (res) {
        console.log(res.data)
        console.log('----------------------------')
        _this.setData({
          phone: res.data
        })
      }
    })
    //获取公司信息
    
    wx.request({
      url: app.globalData.path + 'services/app/applets/GetCompanyInfo',
      data: {
        tenantName: app.globalData.tenancyName
      },
      method: 'POST',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.success) {
          _this.setData({
            companyName: res.data.result.companyName,
            logoPath: app.globalData.logoPath+res.data.result.logoPath
          })
          app.globalData.agreementTitle = res.data.result.agreementTitle;
          app.globalData.agreementContent = res.data.result.agreementContent;
          // console.log(res.data.result.agreementTitle)
          // console.log(res.data.result.agreementContent)
          // console.log('----------------------------------------------------------')
        } else {
          wx.showToast({
            title: res.data.error.message,
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail: function (res) {
        console.log(res.error.message);
      }
    })
  },
  toRegister:function(){
    wx.reLaunch({
      url:"../register/register"
    })
  },
  formSubmit:function(e){
    const _this = this;
    console.log(e.detail.value.phone);
    console.log(e.detail.value.pwd);
    this.setData({
      phone:e.detail.value.phone,
      pwd:e.detail.value.pwd
    })
    if(!(this.data.phone && this.data.pwd)){
        wx.showModal({
          content: '手机号和密码不能为空！',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            }
          }
        });
    }else{
      _this.setData({
        loading:true
      })
      console.log('------------------21');
      wx.request({
        url: app.globalData.path + 'services/WeChatMiniProgramLogin',
        data: {
          usernameOrEmailAddress: e.detail.value.phone,
          password: e.detail.value.pwd,
          tenancyName: app.globalData.tenancyName
        },
        method: 'POST',
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          console.log(res.data);
          _this.setData({
            loading: false
          })
          if(res.data.success){
            if (res.data.result.token) {
              app.globalData.authorization = res.data.result.token;
              if (!res.data.result.isBind) {
                // 未绑定银行卡
                wx.reLaunch({
                  url: '../addcard/addcard?phone=' + _this.data.phone,
                })
              } else if (!res.data.result.isSetPayPassword) {
                // 未绑定支付密码
                wx.reLaunch({
                  url: '../paypwd/paypwd',
                })
              } else {
                wx.switchTab({
                  url: '../index/index?phone=' + e.detail.value.phone
                })
                wx.setStorage({
                  key: 'loginPhone',
                  data: e.detail.value.phone,
                })
                let now = new Date().getTime();
                console.log(now);
                wx.setStorage({
                  key: 'timeKeep',
                  data: now,
                })
                wx.setStorage({
                  key: 'token',
                  data: res.data.result.token,
                })
              }
            } else {
              wx.showModal({
                content: '手机号或密码错误！',
                showCancel: false,
                success: function (res) {
                  if (res.confirm) {
                    // console.log('用户点击确定')
                  }
                }
              });
            }
          }else{
            wx.showToast({
              title: res.data.error.message,
              icon: 'none',
              duration: 2000
            })
          }
        },
      })
    }
  },
  toForget:function(){
    wx.navigateTo({
      url: '../forgetpwd/forgetpwd',
    })
  }
});